// pages/butorder/butorder.js
const app = getApp();
var utils = require('../../utils/util.js');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		addressname:"添加地址"
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {

		//	2. 履约保函     3.预付款保函     4.农民工保函   1.投标保函
		if (app.globalData.type == 2) {
			wx.setNavigationBarTitle({
				title: '履约保函'
			})
		}
		if (app.globalData.type == 3) {
			wx.setNavigationBarTitle({
				title: '预付款保函'
			})
		}
		if (app.globalData.type == 4) {
			wx.setNavigationBarTitle({
				title: '农民工保函'
			})
		}
		if (app.globalData.type == 1) {
			wx.setNavigationBarTitle({
				title: '投标保函'
			})
		}
		this.getLetterDetail();
		this.addressDetail();
	},
	onShow:function(){
		this.addressDetail();
	},
	//获取保函详情
	getLetterDetail: function (e) {
		var that = this;
		console.log(wx.getStorageSync("detail"))
		utils.http("/api/v1/common/get_letter_detail", {
			letter_id: wx.getStorageSync("detail").letter_id
		}, function (res) {
			var price = parseFloat(res.datas.letter_rate) * parseFloat(wx.getStorageSync("detail").letter_price) * 10000;
			that.setData({
				detail: res.datas,
				allprice: parseFloat(parseFloat(price).toFixed(4)) 
			})
		})
	},
	//获取地址信息
	addressDetail:function(e){
		var that=this;
		if (app.globalData.addressChid != undefined){
			//获取默认地址
			that.setData({
				address_id: app.globalData.addressChid
			})
			
			that.getAddressDetails();	
		}else{
			that.setData({
				address_id:""
			})
			that.getAddressDetails();	
		}
	},
	//修改调用地址详情
	getAddressDetails: function () {
		var that = this;
		console.log(app.globalData.addressChid)
		utils.http("/api/v1/address/address_info", {
			address_id:that.data.address_id
		}, function (res) {
			that.setData({
				addressinfo:res.datas,
				addressname: res.datas.address + "" + res.datas.detail
			})
		})
	},
	onMyAddressTap:function(e){
		wx.navigateTo({
			url: '../myaddress/myaddress'
		})
	},
	//去支付
	formSubmit:function(e){
		var that=this;
		if (e.detail.value.letter_sn==""){
			wx.showToast({
				title: '请输入投标编号',
				icon:"none"
			})
			return false;
		}
		if (e.detail.value.letter_project_name == "") {
			wx.showToast({
				title: '请输入项目名称',
				icon: "none"
			})
			return false;
		}
		if (that.data.addressinfo==undefined){
			wx.showToast({
				title: '请选择收货地址',
				icon: "none"
			})
			return false;
		}
		if (e.detail.value.letter_toubiaoren==""){
			wx.showToast({
				title: '请输入投标人',
				icon: "none"
			})
			return false;
		}
		if (e.detail.value.letter_shouyiren == "") {
			wx.showToast({
				title: '请输入受益人',
				icon: "none"
			})
			return false;
		}
		var info={
			address_id: that.data.addressinfo.id,
			letter_project_name:  e.detail.value.letter_project_name,
			letter_shouyiren: e.detail.value.letter_shouyiren,
			letter_sn: e.detail.value.letter_sn,
			letter_toubiaoren: e.detail.value.letter_toubiaoren,
			price: that.data.allprice
		}
		wx.setStorageSync("detail", Object.assign(wx.getStorageSync("detail"), info));
		wx.navigateTo({
			url: "../butorderpayment/butorderpayment",
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})